<script setup lang="ts">
import { useRouterPush } from '@/composables';
import { useAuthStore } from '@/store';
import { useGetAuthInfo } from '@/hooks';
import { SUCCESS_MSG_DURATION } from '@/constants';
import { storeToRefs } from 'pinia';
import { ref } from 'vue';
import { showToast, showLoading, hideLoading } from '@tarojs/taro';
import { useNurseUser } from '@/service';
// import BasicMap from '@/components/basic-map/index.vue';

definePageConfig({
    navigationBarTitleText: '被监护人地址'
});

const { nurseUser } = storeToRefs(useAuthStore());

const { deleteAddr } = useNurseUser();

const { toAddr } = useRouterPush();

const dataOptions = {
    id: 'id',
    addressName: 'name',
    phone: 'phone',
    defaultAddress: 'isDefault',
    fullAddress: 'fullAddress'
};

const visible_delete = ref(false);

const _id = ref();

const clickItem = (_e: any, item: { id: string | undefined }) => {
    toAddr(item.id);
};
const editClick = (_e: any, item: { id: string | undefined }) => {
    toAddr(item.id);
};
const delClick = (_e: any, item: { id: string | undefined }) => {
    visible_delete.value = !visible_delete.value;
    _id.value = item.id;
};
const delClickSwipe = (_e: any, item: { id: string | undefined }) => {
    visible_delete.value = !visible_delete.value;
    _id.value = item.id;
};
async function onDelete() {
    showLoading({ title: '正在删除...' });
    const { error } = await deleteAddr(_id.value);
    hideLoading();
    if (!error) {
        showToast({
            title: '删除成功',
            icon: 'success',
            duration: SUCCESS_MSG_DURATION
        });
        useGetAuthInfo();
    }
}
</script>
<template>
    <basic-layout>
        <!-- <custom-navbar title="我的地址" showLeft /> -->
        <nut-empty v-if="!nurseUser.nurseUserAddrList || nurseUser.nurseUserAddrList.length == 0" description="暂无地址"></nut-empty>
        <view class="w-375px text-primary b-rounded">
            <nut-address-list :data="nurseUser.nurseUserAddrList || []" swipe-edition :show-bottom-button="false"
                :data-options="dataOptions" @edit-icon="editClick" @del-icon="delClick" @click-item="clickItem"
                @swipe-del="delClickSwipe">
            </nut-address-list>
            <basic-button text="新增地址" class="m-t-2" @on-click="toAddr" />
        </view>
        <nut-dialog v-model:visible="visible_delete" title="提示" content="是否删除该地址？" @cancel="visible_delete = false"
            @ok="onDelete" />
    </basic-layout>
</template>
